const express = require('express')
const db = require('../db/db')


const router = express.Router()

router.get(['/', '/k-:keyword([A-Za-z0-9]{3,20})'], (req, res) => {
    const q = db.Question.listByPager(req.params.keyword)
    const k = db.Keyword.list()

    Promise.all([q, k])
        .then(([questions, keywords]) => {
            res.render('index', {
                keyword: req.params.keyword,
                account: req.cookies.account,
                keywords,
                questions
            })
        })
        .catch(err => {
            console.error('获取 questions 或 keywords 错误：', err)
            res.render('error', err)
        })
})


module.exports = router